//
//  NSString+DFNumber.m
//  FrameWork
//
//  Created by 王*风 on ****/5/9.
//

#import "NSString+DFNumber.h"

@implementation NSString (DFNumber)

/** 金额数字加入千位分隔符*/
+ (NSString *)df_moneyNumJoinThousandSegmentationWithStr:(NSString *)digitString
{
    NSLog(@"%@", [digitString componentsSeparatedByString:@"."]);
    if (PCBStringIsEmpty(digitString))
    {
        return @"";
    }
  
    if (![digitString containsString:@"."])
    {
        digitString=[NSString stringWithFormat:@"%@.00",digitString];
    }
    NSArray *array = [digitString componentsSeparatedByString:@"."];
  
    digitString=array[0];
    NSString *pointStr=[array[1]description];
    if (digitString.length <= 3) {
        return [NSString stringWithFormat:@"%@%@%@",digitString,@".",pointStr];
    } else {
        NSMutableString *processString = [NSMutableString stringWithString:digitString];
        
        // 1
        int location = (int)processString.length - 3;
        NSMutableArray *processArray = [NSMutableArray array];
        while (location >= 0) {
            NSString *temp = [processString substringWithRange:NSMakeRange(location, 3)];
            
            [processArray addObject:temp];
            if (location < 3 && location > 0)
            {
                NSString *t = [processString substringWithRange:NSMakeRange(0, location)];
                [processArray addObject:t];
            }
            location -= 3;
        }
        //    NSLog(@"processArray = %@",processArray);
        
        // 2
        NSMutableArray *resultsArray = [NSMutableArray array];
        int k = 0;
        for (NSString *str in processArray)
        {
            k++;
            NSMutableString *tmp = [NSMutableString stringWithString:str];
            if (str.length > 2 && k < processArray.count )
            {
                [tmp insertString:@"," atIndex:0];
                [resultsArray addObject:tmp];
            } else {
                [resultsArray addObject:tmp];
            }
        }
        //    NSLog(@"resultsArray = %@",resultsArray);
        
        // 3
        NSMutableString *resultString = [NSMutableString string];
        for (int i = (int)resultsArray.count - 1 ; i >= 0; i--)
        {
            NSString *tmp = [resultsArray objectAtIndex:i];
            [resultString appendString:tmp];
        }
        //    NSLog(@"resultStr = %@",resultString);
        return [NSString stringWithFormat:@"%@%@%@",resultString,@".",pointStr];
    }
}

/** 金额数字去除千位分隔符*/
+(NSString*)df_moneyNumDeleteThousandSegmentation:(NSString*)money
{
    
    NSArray *array = [money componentsSeparatedByString:@","];
    NSString *result=@"";
    for (int i=0; i<array.count; i++) {
        result=[result stringByAppendingString:[array[i]description]];
    }
    return result;
}
/** 每隔N个数字,使用逗号进行隔开*/
+(NSString*)df_numSegmentationNumString:(NSString *)num intervalNum:(NSInteger)intervalNum
{
    if (num.length<intervalNum)
    {
        return  num;
    }else
    {
        NSString *str = [num substringWithRange:NSMakeRange(num.length%intervalNum, num.length-num.length%intervalNum)];
        NSString *strs = [num substringWithRange:NSMakeRange(0, num.length%intervalNum)];
        for (NSInteger  i =0; i < str.length; i =i+intervalNum)
        {
            NSString *sss = [str substringWithRange:NSMakeRange(i, intervalNum)];
            strs = [strs stringByAppendingString:[NSString stringWithFormat:@",%@",sss]];
        }
        if ([[strs substringWithRange:NSMakeRange(0, 1)] isEqualToString:@","]) {
            strs = [strs substringWithRange:NSMakeRange(1, strs.length-1)];
        }
        return strs;
        
    }
}
/** 去掉小数点后面的0*/
+(NSString*)df_deleteNumberLastZero:(double)stringFloat
{
    const char*floatChars=[[NSString stringWithFormat:@"%.2f",stringFloat] UTF8String];
    NSUInteger length=[[NSString stringWithFormat:@"%.2f",stringFloat] length];
    NSUInteger zeroLength = 0;
    NSInteger i = length-1;
    for (; i>0; i--)
    {
        if (floatChars[i] == '0'/*0x30*/)
        {
            zeroLength++;
        }else
        {
            if(floatChars[i] == '.')
                i--;
            break;
        }
    }
    
    NSString *returnString;
    if(i == -1) {
        returnString = @"0";
    } else {
        returnString = [[NSString stringWithFormat:@"%.2f",stringFloat] substringToIndex:i+1];
    }
    return returnString;
}
/** 保留小数点后面N位数字,但是不进行四舍五入*/
+(NSString*)df_keepNumLastBitsNoRounded:(NSString*)conversionStr keepNum:(NSInteger)keepNum
{
    double num=[conversionStr doubleValue];
    NSNumber*number=@(num);
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.roundingMode = NSNumberFormatterRoundFloor;
    formatter.maximumFractionDigits =keepNum;
    NSString*lastStr=[NSString stringWithFormat:@"%@",[formatter stringFromNumber:number]];
    return lastStr;
}

/** 不四舍五入小数*/
+(NSString *)df_notRounding:(float)price afterPoint:(int)position
{
    NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    NSDecimalNumber *ouncesDecimal;
    NSDecimalNumber *roundedOunces;
    
    ouncesDecimal = [[NSDecimalNumber alloc] initWithFloat:price];
    roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
    
    return [NSString stringWithFormat:@"%0.2f",[roundedOunces doubleValue]];
}

/** 获取一个随机整数，范围在[from,to），包括from，不包括to*/
+(int)df_getToRandomNumber:(int)from to:(int)to
{
    return (int)(from + (arc4random() % (to-from + 1)));
}

/** 从数字或者字母中随机生成一个随机数 */
+(NSString*)df_getRandomStringWithNum:(NSInteger)num
{
    
    NSString *string = [[NSString alloc]init];
    for (int i = 0; i < num; i++) {
        int number = arc4random() % 36;
        if (number < 10) {
            int figure = arc4random() % 10;
            NSString *tempString = [NSString stringWithFormat:@"%d", figure];
            string = [string stringByAppendingString:tempString];
        }else {
            int figure = (arc4random() % 26) + 97;
            char character = figure;
            NSString *tempString = [NSString stringWithFormat:@"%c", character];
            string = [string stringByAppendingString:tempString];
        }
    }
    return string;

}

@end
